<?php
/**
 * MessageGateway Mail
 * @author seraph.liujb 2009.07.15
 *
 */
/**
 * 应用示例
 $params = array(
 'url'=>'smtp.qq.com',
 'port'=>465,
 'auth'=>'login',
 'ssl'=>'ssl',
 'username'=>'seraph.liujb@qq.com',
 'password'=>'123456',
 );
 $mg = Common_MessageGateway::factory('Mail',$params);
 $mg->setFrom('seraph.liujb@gmail.com');
 $mg->setTo('toMail@domain.com','domain.com toMail');
 $mg->addTo('ccMail@domain.com','domain.com ccMail');
 $result = $mg->send('mail subject','mail context');
 */

class Common_MessageGateway_Mail
{
	/**
	 * @var $_url 邮件服务器地址，一般为smtp服务
	 * @var $_port 服务端口，一般非加密端口为25，加密端口一般为465,938
	 */
	private $_url = 'smtp.domain.com';
	private $_port = 25;

	/**
	 * @var $_auth 验证类型，默认：login
	 * @var $_ssl 加密类型，非加密为空，加密默认：ssl
	 */
	private $_auth = 'login';
	private $_ssl = 'ssl';

	/**
	 * @var $_username 邮件服务登录帐号
	 * @var $_password 密码
	 */
	private $_username = '';
	private $_password = '';

	/**
	 * 邮件地址设置，array类型，addr为邮件地址，name为邮件所有者名称，默认两者相同
	 * from为单一地址array()，to,cc,bcc为多地址array(0=>array(),1=>array())
	 *
	 * @var array $_from 发件人地址
	 * @var array $_to 收件人地址
	 * @var array $_cc 抄送地址
	 * @var array $_bcc 密送地址
	 */
	private $_from = array('addr'=>'mailer@domain.com','name'=>'domain.com mailer');
	private $_to = array(0=>array('addr'=>'mailto@domain.com','name'=>'domain.com mailto'));
	private $_cc = array();
	private $_bcc = array();

	/**
	 * 初始化处理
	 *
	 * @param array $params 初始化参数
	 * url 邮件服务地址 smtp.domain.com
	 * auth 验证类型 login
	 * username 帐户
	 * password 密码
	 * ssl 加密类型 ssl
	 * port 端口 25 465 938
	 */
	public function __construct($params = array())
	{
		//设置参数
		if (!is_null($params['url'])) $this->_url = $params['url'];
		if (!is_null($params['port'])) $this->_port = $params['port'];
		if (!is_null($params['auth'])) $this->_auth = $params['auth'];
		if (!is_null($params['ssl'])) $this->_ssl = $params['ssl'];
		if (!is_null($params['username']) && !is_null($params['password'])) $this->setAccount($params['username'],$params['password']);
		//
		$this->_logger = Common_Logger::factory(array('module'=>'Common_MessageGateway_Mail'));
	}

	/**
	 * 设置帐密
	 *
	 * @param string $username
	 * @param string $password
	 * @return object
	 */
	public function setAccount($username,$password)
	{
		$this->_username = $username;
		$this->_password = $password;
		return $this;
	}

	/**
	 * 设置发件人地址
	 *
	 * @param string $addr
	 * @param string $name
	 * @return object
	 */
	public function setFrom($addr,$name)
	{
		$this->_from = array(
            'addr'=>$addr,
            'name'=>$name,
		);
		return $this;
	}

	/**
	 * 设置收件人地址
	 *
	 * @param string $addr
	 * @param string $name
	 * @return object
	 */
	public function setTo($addr,$name)
	{
		$this->_to[0] = array('addr'=>$addr,'name'=>$name);
		return $this;
	}

	/**
	 * 追加收件人地址
	 *
	 * @param string $addr
	 * @param string $name
	 * @return object
	 */
	public function addTo($addr,$name)
	{
		$to = array('addr'=>$addr,'name'=>$name);
		$this->_to[] = $to;
		return $this;
	}

	/**
	 * 简洁邮件发送接口，强制发送html型内容
	 *
	 * @param string $subject 邮件主题
	 * @param string $context 邮件内容
	 *
	 * @return boolean
	 */
	public function send($subject = '无主题',$context = '无内容')
	{
		//初始化mail transport
		$transport = new Zend_Mail_Transport_Smtp($this->_url,array(
            'port'=>$this->_port,
            'auth'=>$this->_auth,
            'ssl'=>$this->_ssl,
            'username'=>$this->_username,
            'password'=>$this->_password,
		));
		//
		$mail = new Zend_Mail('utf-8');
		$mail->setFrom($this->_from['addr'],$this->_from['name']);
		foreach ($this->_to as $to) {
			$mail->addTo($to['addr'],$to['name']);
		}
		$mail->addTo($this->_to);
		$mail->setSubject($subject);
		$mail->setBodyHtml($context);
		$result = $mail->send($transport);

		return $result;
	}

}